package com.kamisama.utopia.service;

import com.kamisama.utopia.constant.GUIDE;
import com.kamisama.utopia.constant.map.WorldMap;
import com.kamisama.utopia.dto.CommandDto;
import com.kamisama.utopia.entity.Character;
import com.kamisama.utopia.repository.CharacterRepository;
import com.kamisama.utopia.respond.RespondTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import org.springframework.util.StringUtils;

@Service
public class CharacterService {

    @Autowired
    private CharacterRepository characterRepository;

    public CommandDto characterLogin(String openId, CommandDto commandDto) {
        List<Character> characterIdList = characterRepository.findAllByUserOpenId(openId);
        if (characterIdList.isEmpty()) {
            commandDto.setRespond("您尚未在理想乡注册，请使用命令 注册<角色名> 注册您的身份信息");
            return commandDto;
        } else {
            // Todo 将角色信息存入redis
            String characterIds = "";
//            for (int i = 0; i < characterIdList.size(); i++) {
//                Character character = characterIdList.get(i);
//                characterIds = character.getId().toString() + ",";
//            }
            characterIds = characterIdList.get(0).getId().toString();
            commandDto.setCharacterId(characterIds);
            commandDto.setRespond("登录成功");
            return commandDto;
        }
    }

    public CommandDto registCharacter(CommandDto commandDto, String name) {
        String openId = commandDto.getOpenId();
        int characterCount =  characterRepository.countAllByUserOpenId(openId);
        if (characterCount < 1) {
            Character character = Character.builder().userOpenId(openId).name(name).build().initialization();
            Character characterSave = characterRepository.save(character);
            commandDto.setCharacterId(characterSave.getId().toString());
            commandDto.setRespond(name + "理想乡身份证办理成功,其他命令请使用命令 #新手指南 查看");
            return commandDto;
        } else {
            commandDto.setRespond(name + "目前每个微信号最多创建一个角色");
            return commandDto;
        }
    }

    public String getCharacter(String id, String openId) {
        Character character = characterRepository.findByIdAndUserOpenId(Long.valueOf(id), openId);
        if (ObjectUtils.isEmpty(character)) {
            return "不存在该角色";
        } else {
            return RespondTemplate.formatCharacterRespond(character);
        }
    }

    public String getBeginGuide() {
        return GUIDE.BEGIN_GUIDE;
    }

    public String getMap(String id, String openId) {
        Character character = characterRepository.findByIdAndUserOpenId(Long.valueOf(id), openId);
        return WorldMap.getFormatMap(character.getLocation());
    }

    public String moveTo(String id, String openId, String target) {
        Character character = characterRepository.findByIdAndUserOpenId(Long.valueOf(id), openId);
        String[] strings = StringUtils.delimitedListToStringArray(character.getLocation(), "-");
        Double cost = WorldMap.getMoveCost(strings, target);
        if (cost > 0) {
            character.getAttribute().setSatiety(character.getAttribute().getSatiety() - cost);
            strings[2] = target;
            character.setLocation(StringUtils.arrayToDelimitedString(strings, "-"));
//            saveCharacter(character); Todo
            characterRepository.save(character);
            return "";
        } else if (cost == 0) {

        } else {

        }
        return "";
    }
}
